package com.zhongketech.sunflower.app;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import com.zhongketech.sunflower.Constant;
import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.droidwall.DroidWallHandle;
import com.zhongketech.sunflower.sv.SvManager;
import com.zhongketech.sunflower.transport.Action;
import com.zhongketech.sunflower.transport.LogUtil;
import com.zhongketech.sunflower.transport.model.MyApplicationsInfo;
import com.zhongketech.sunflower.transport.model.Parents;
import com.zhongketech.sunflower.utils.Utilities;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;

public class AppManager {

	private final static String TAG = "AppManager";
	private static AppManager instance = null;
	private static  Context context = null;
	private ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); //用来存储获取的应用信息数据
	private static String curPackageName = null;

	private AppManager()
	{

	}

	public static AppManager getInstance()
	{
		if(null == instance)
		{
			instance = new AppManager();
			instance.context = MyApplication.getInstance().getApplicationContext();
			if(!thread.isAlive())
			{
				LogUtil.i(TAG,"app check thread start");
				thread.start();	
			}
		}

		return instance;
	}


	private void initInstallApp()
	{
		boolean isContinue = false;
		List<PackageInfo> packages = context.getPackageManager().getInstalledPackages(0);
		if(null != appList)
		{
			appList.clear();
		}

		for(int i=0;i<packages.size();i++) 
		{ 
			PackageInfo packageInfo = packages.get(i);
			isContinue = false;

			String hideList[] = {"com.zhongketech.sunflower","eu.chainfire.supersu","com.android.systemui"};

			if(null == context.getPackageManager().getLaunchIntentForPackage(packageInfo.packageName))
			{
				continue;
			}


			for(int k = 0;k < hideList.length;k++)
			{
				if(hideList[k].equals(packageInfo.packageName))
				{
					isContinue = true;
					break;
				}
			}

			if(isContinue)
			{
				continue;
			}

			final AppInfo tmpInfo =new AppInfo(); 
			tmpInfo.appName = packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString(); 
			tmpInfo.packageName = packageInfo.packageName; 
			tmpInfo.versionName = packageInfo.versionName; 
			tmpInfo.versionCode = packageInfo.versionCode; 
			tmpInfo.appIcon = packageInfo.applicationInfo.loadIcon(context.getPackageManager());
			//check this app is stop ??
			//			if(Data.getinstance().isExit(new ApplicationData(tmpInfo.packageName, tmpInfo.appName),Data.DATATYPE_PHONEBLACKLIST))
			//			{
			//				tmpInfo.isStop = true;
			//			}
			//			else
			//			{
			//				tmpInfo.isStop = false;
			//			}

			appList.add(tmpInfo);	
		}
	}


	static Thread thread = new Thread(new Runnable()  
	{  
		@Override  
		public void run()  
		{   
			Looper.prepare();
			instance.initInstallApp();

			while(true)
			{
				// TODO Auto-generated method stub  
				try {
					Thread.sleep(Constant.APP_STOP_INTERVAL);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				if(SvManager.getInstance().isWebEnable())
				{
					DroidWallHandle.getInstance(MyApplication.getInstance().getApplicationContext()).enableNetLock();
				}
				else
				{
					DroidWallHandle.getInstance(MyApplication.getInstance().getApplicationContext()).diableNetLock();
				}
				
				//
				//				//				LogUtil.i(TAG,"app check thread start  .....");
				final String packages = getRunPackage();
				////				if(packages.equals(curPackageName))
				////				{
				////					continue;
				////				}
				////				
				//				curPackageName = packages;
				final String[] lockNoKilAPP = {"com.android.systemui","com.zhongketech.sunflower",
						"om.android.defcontainer",
				"system"}; 
				//				
				if(SvManager.getInstance().isLock())
				{
					boolean kill = true;
					//					LogUtil.i(TAG,"lockPhone");
					for(int i = 0;i < lockNoKilAPP.length;i ++)
					{
						if(packages.startsWith(lockNoKilAPP[i]))
						{
							kill = false;
							//							LogUtil.i(TAG,"nu kill:"+packages);
							break;
						}
					}

					if(kill)
					{
						MyToast.getInstance().lockPhone();
						Utilities.killProcess(packages, context);	
					}

				}
				else if(!SvManager.getInstance().isOpen(packages))
				{
					//					LogUtil.i(TAG,"can't start app");
					MyToast.getInstance().showBox(packages);
					Utilities.killProcess(packages, context);
				}
			}



		}  
	});  


	private static String getRunPackage()
	{
		final ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);  
		final ComponentName cn = am.getRunningTasks(1).get(0).topActivity;  
		return  cn.getPackageName();
	}


	//	/**
	//	 * 
	//	 * @return 
	//	 */
	//	public static List<String> getALLStopApp()
	//	{
	//		List<String> list= new ArrayList<String>();
	//		
	//		return list;
	//	}
	//	

	public void uploadapp()
	{

		new Thread(new Runnable() {
			public void run() {
				Looper.prepare();
				getAppList();
				for(int i = 0;i < appList.size();i++)
				{
					MyApplicationsInfo appInfo = new MyApplicationsInfo();
					appInfo.setApp_name(appList.get(i).appName);
					appInfo.setPackage_name(appList.get(i).packageName);
					appInfo.setImage_url(null);
					new Action().uploadApp(appInfo);	
				}


			}
		}).start();
	}

	public int getCount()
	{
		return appList.size();
	}

	public Drawable getAppIcon(int index)
	{
		return appList.get(index).appIcon;
	}


	public String getAppName(int index)
	{
		return appList.get(index).appName;
	}

	public String getAppPackage(int index)
	{
		if(index >= appList.size())
		{
			return null;
		}

		return appList.get(index).packageName;
	}

	public List<AppInfo> getAppList()
	{		
		return appList;
	}



	public boolean setAppInfoStopFlag(String packageName,boolean flag)
	{
		getAppInfo(packageName).isStop = flag;
		//		if(flag)
		//		{
		//			Data.getinstance().add(new ApplicationData(getAppInfo(packageName).packageName, getAppInfo(packageName).appName),Data.DATATYPE_STOPAPP);
		//		}
		//		else
		//		{
		//			Data.getinstance().del(new ApplicationData(getAppInfo(packageName).packageName, getAppInfo(packageName).appName),Data.DATATYPE_STOPAPP);
		//		}

		return true;
	}

	public boolean setAppInfoStopFlag(int index,boolean flag)
	{
		if(index >= appList.size())
		{
			return false;
		}

		appList.get(index).isStop = flag;
		//		if(flag)
		//		{
		//			Data.getinstance().add(new ApplicationData(appList.get(index).packageName, appList.get(index).appName),Data.DATATYPE_STOPAPP);
		//		}
		//		else
		//		{
		//			Data.getinstance().del(new ApplicationData(appList.get(index).packageName, appList.get(index).appName),Data.DATATYPE_STOPAPP);
		//		}



		return true;
	}


	public AppInfo getAppInfo(int index)
	{
		if(index >= appList.size())
		{
			return null;
		}

		return appList.get(index);
	}

	public AppInfo getAppInfo(String packages)
	{
		for(int i = 0;null != packages && i < appList.size(); i ++)
		{
			if(packages.equals(appList.get(i).packageName))
			{
				return appList.get(i);
			}
		}

		return null;
	}

}
